#include "proto.h"

int main (void)
{
	struct sockaddr_in remote_addr;
	struct ip_mreqn imr;
	struct msg_t buf;

	int fd = socket (AF_INET, SOCK_DGRAM, 0);
	if (-1 == fd) {
		perror ("socket ()");
		exit (1);
	}

	//加入多播组
	inet_aton (GROUP_IP, &imr.imr_multiaddr);
	inet_aton ("0.0.0.0", &imr.imr_address);
	imr.imr_ifindex = if_nametoindex ("ens33");
	if (-1 == setsockopt (fd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, &imr, sizeof (imr)))
	{
		perror ("setscokopt()");
		return -1;
	}
	
	remote_addr.sin_family = AF_INET;
	remote_addr.sin_port = htons (atoi(RCV_PORT));
	inet_aton (GROUP_IP, &remote_addr.sin_addr);

	while (1) {
		//向多播组中发送数据
		bzero (&buf, MAGSIZE);
		strncpy (buf.str, "hello", MAGSIZE);
		sendto (fd, &buf, strlen(buf.str) + 1, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
		sleep (1);
	}
	close (fd);
	return 0;
ERROR:
	close (fd);
}
